Parcourir la source

Adapt ending of activities; change bg task handling

Deniz Cengiz il y a 1 an
Parent
commit
3ba6316b3c

+ 20 - 1
Trio/Sources/Services/LiveActivity/LiveActivityManager.swift

@@ -352,14 +352,25 @@ final class LiveActivityManager: Injectable, ObservableObject, SettingsObserver
 
     /// Ends the current live activity and ensures that all unknown activities are terminated.
     private func endActivity() async {
+        debug(.default, "Ending all live activities...")
+
         if let currentActivity {
+            debug(.default, "Ending current activity: \(currentActivity.activity.id)")
             await currentActivity.activity.end(nil, dismissalPolicy: .immediate)
             self.currentActivity = nil
         }
 
+        for activity in Activity<LiveActivityAttributes>.activities {
+            debug(.default, "Ending lingering activity: \(activity.id)")
+            await activity.end(nil, dismissalPolicy: .immediate)
+        }
+
         for unknownActivity in Activity<LiveActivityAttributes>.activities {
+            debug(.default, "Ending unknown activity: \(unknownActivity.id)")
             await unknownActivity.end(nil, dismissalPolicy: .immediate)
         }
+
+        debug(.default, "All live activities ended.")
     }
 
     /// Restarts the live activity from a Live Activity Intent.
@@ -370,7 +381,7 @@ final class LiveActivityManager: Injectable, ObservableObject, SettingsObserver
         guard let latestGlucose = latestGlucose,
               let determination = determination
         else {
-            debug(.default, "Cannot restart live activity because required persistent state is not available")
+            debug(.default, "Cannot restart live activity because required persistent state is not available. Fetching data...")
             return
         }
 
@@ -389,6 +400,14 @@ final class LiveActivityManager: Injectable, ObservableObject, SettingsObserver
         }
 
         await endActivity()
+
+        while (currentActivity != nil && currentActivity!.activity.activityState != .ended) || Activity<LiveActivityAttributes>
+            .activities.contains(where: { $0.activityState != .ended })
+        {
+            debug(.default, "Waiting for Live Activity to end...")
+            try? await Task.sleep(nanoseconds: 200_000_000) // 0.2s sleep
+        }
+
         await pushUpdate(contentState)
         debug(.default, "Restarted Live Activity from LiveActivityIntent (via iOS Shortcut)")
     }

+ 20 - 12
Trio/Sources/Shortcuts/LiveActivity/RestartLiveActivityIntentRequest.swift

@@ -11,25 +11,33 @@ import UIKit
     /// - Throws: An error if the restart process fails.
     /// - Returns: Void upon successful restart.
     @MainActor func performRestart() async throws {
-        // Start background task
         var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
-        backgroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "Restart Live Activity") {
-            guard backgroundTaskID != .invalid else { return }
-            Task {
-                UIApplication.shared.endBackgroundTask(backgroundTaskID)
-            }
-            backgroundTaskID = .invalid
-        }
 
-        defer {
-            if backgroundTaskID != .invalid {
-                Task {
+        // Start background task
+        backgroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "Restart Live Activity") {
+            Task { @MainActor in
+                if backgroundTaskID != .invalid {
                     UIApplication.shared.endBackgroundTask(backgroundTaskID)
+                    backgroundTaskID = .invalid
+                    debug(.default, "Background task expired and ended.")
                 }
-                backgroundTaskID = .invalid
             }
         }
 
+        guard backgroundTaskID != .invalid else {
+            debug(.default, "Failed to start background task.")
+            return
+        }
+
+        debug(.default, "Background task started: \(backgroundTaskID)")
+
         await liveActivityManager.restartActivityFromLiveActivityIntent()
+
+        // Ensure background task ends properly
+        if backgroundTaskID != .invalid {
+            UIApplication.shared.endBackgroundTask(backgroundTaskID)
+            debug(.default, "Background task ended successfully.")
+            backgroundTaskID = .invalid
+        }
     }
 }